/**
 *
 * @author 
 *
 */
class GameOverView extends eui.Component{
    
    public lb_results1:eui.Label;
    public lb_results2:eui.Label;
    private lb_results3:eui.Label;
    private btn_again:eui.Image;
    private btn_fenxiang:eui.Image;
    private btn_ckphb:eui.Rect;
    private im_ckphb:eui.Image;
    
    private static _instance: GameOverView;
    public static getInstance(): GameOverView {
        if(GameOverView._instance == null) {
            GameOverView._instance = new GameOverView();
        }
        return GameOverView._instance;
    }
    
	public constructor() {
    	  super();
        this.skinName = "resource/skins/GameOverViewSkin.exml";
	}
	
    public createChildren() {
        super.createChildren();
        this.btn_again.addEventListener(egret.TouchEvent.TOUCH_TAP,this.btnClick,this);
        this.btn_fenxiang.addEventListener(egret.TouchEvent.TOUCH_TAP,this.btnClick,this);
        this.btn_ckphb.addEventListener(egret.TouchEvent.TOUCH_TAP,this.btnClick,this);
        this.init();
    }
    
    private btnClick(ev:egret.TouchEvent){
        switch(ev.target){
            case this.btn_again:
                GameScene.getInstance().init();
                Main.getInstance().addChild(GameScene.getInstance());
                Main.getInstance().removeChild(GameOverView.getInstance());
                Data.grade = 0;
            break;
            case this.btn_fenxiang:
                var fen:FenXiangView = new FenXiangView();
                Main.getInstance().addChild(fen);
            break;
            case this.btn_ckphb:
                if(Main.getInstance().contains(GameOverView.getInstance())) {
                    GameOverView.getInstance().parent.removeChild(GameOverView.getInstance());
                }
                Main.getInstance().addChild(PHBView.getInstance());
                this.sendForPHB();
            break;
        }
    }
    
    private sendForPHB() {
        //给后端发请求
        var openid = egret.localStorage.getItem("openid");
        var request1 = new egret.HttpRequest();
        request1.responseType = egret.HttpResponseType.TEXT;
        request1.open("http://ztetj7.weixinzjit.com/ZTE/index.php/User/info?" + "openid=" + openid,egret.HttpMethod.GET);
        request1.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        request1.send();
        request1.addEventListener(egret.Event.COMPLETE,this.onGetComplete3,this);
        request1.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onGetIOError3,this);
        request1.addEventListener(egret.ProgressEvent.PROGRESS,this.onGetProgress3,this);
    }

    private onGetComplete3(event: egret.Event): void {
        var request = <egret.HttpRequest>event.currentTarget;
        var response = request.response;
        var ob = JSON.parse(response);
        PHBView.getInstance().myPM.text = ob.rank + "位";
        PHBView.getInstance().setData(ob.data);
    }
    private onGetIOError3(event: egret.IOErrorEvent): void {

    }
    private onGetProgress3(event: egret.ProgressEvent): void {
        //        alert("get error : " + event);
    }
    
    private init() {
        egret.Tween.get(this,{ loop: true }).to({ t: Math.PI * 2 },800);
    }
    private _t: number = 0;
    get t(): number { return this._t; }
    set t(value: number) {
        this._t = value;
        this.btn_again.scaleX = Math.sin(value) * 0.05 + 1;
        this.btn_again.scaleY = Math.sin(value - Math.PI / 2) * 0.05 + 1;
        this.btn_fenxiang.scaleX = Math.sin(value) * 0.05 + 1;
        this.btn_fenxiang.scaleY = Math.sin(value - Math.PI / 2) * 0.05 + 1;
        this.im_ckphb.scaleX = Math.sin(value) * 0.05 + 1;
        this.im_ckphb.scaleY = Math.sin(value - Math.PI / 2) * 0.05 + 1;
    }
}
